using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Emlyn.Applications.Scratch.Graphics.Tools;
using Microsoft.DirectX.Direct3D;

namespace Emlyn.Applications.Scratch.Graphics.Chapter6
{
    public partial class frmChapter6 : Form
    {
        public frmChapter6()
        {
            InitializeComponent();
        }

        DirectXWrapper _wrapper = null;

        Bitmap _loadedBitmap = null;

        Surface _loadedSurface = null;

        int ScrollX = 0;
        int ScrollY = 0;
        int SpeedX = 0;
        int SpeedY = 0;

        private void timer1_Tick(object sender, EventArgs e)
        {
            ScrollScreen();

            _wrapper.Present();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            _loadedBitmap = new Bitmap(@"x:\emlyn\images\pipeline iso tileset.bmp");

            InitWrapper();

            _wrapper._device.DeviceLost += new EventHandler(DeviceLostHandler);
        }

        private void DisposeWrapper()
        {
            //if (_loadedImage != null && !_loadedImage.Disposed)
            //    _loadedImage.Dispose();
            if (_wrapper != null && _wrapper._backbuffer != null)
                _wrapper._backbuffer.Dispose();
        }

        private void InitWrapper()
        {
            DisposeWrapper();

            _wrapper = new DirectXWrapper(this.ClientSize.Width, this.ClientSize.Height, this);

            _loadedSurface = _wrapper.LoadSurfaceFromBitmap(_loadedBitmap);

            timer1.Enabled = true;
        }

        private void DeviceLostHandler(object sender, EventArgs e)
        {
            InitWrapper();
        }

        private void ScrollScreen()
        {
            ScrollX += SpeedX;
            if (ScrollX < 0)
            {
                ScrollX = 0;
                SpeedX = 0;
            }
            else if (ScrollX > _loadedBitmap.Width - _wrapper._backbuffer.Description.Width)
            {
                ScrollX = _loadedBitmap.Width - _wrapper._backbuffer.Description.Width;
                SpeedX = 0;
            }

            ScrollY += SpeedY;
            if (ScrollY < 0)
            {
                ScrollY = 0;
                SpeedY = 0;
            }
            else if (ScrollY > _loadedBitmap.Height - _wrapper._backbuffer.Description.Height)
            {
                ScrollY = _loadedBitmap.Height - _wrapper._backbuffer.Description.Height;
                SpeedY = 0;
            }

            Rectangle lLoadedSurfaceRect = 
                new Rectangle(
                    ScrollX,
                    ScrollY,
                    ScrollX + _wrapper._backbuffer.Description.Width-1,
                    ScrollY + _wrapper._backbuffer.Description.Height-1
                    );
            _wrapper.DrawOntoBackBuffer(_loadedSurface, lLoadedSurfaceRect, new Point(0, 0));
        }

        private void frmChapter6_FormClosed(object sender, FormClosedEventArgs e)
        {
            timer1.Enabled = false;
            DisposeWrapper();
        }

        private void frmChapter6_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.X < _wrapper._backbuffer.Description.Width / 2)
                SpeedX -= 8;
            else
                SpeedX += 8;
            if (e.Y < _wrapper._backbuffer.Description.Height / 2)
                SpeedY -= 8;
            else
                SpeedY += 8;
        }

        private void frmChapter6_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
                Close();
        }
    }
}